/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package appagenda;

import java.io.OutputStream;
import java.util.Date;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import rmsdao.EventoDAO;

/**
 *
 * @author luis
 */
public class FrmEventoMSG extends Form implements CommandListener {

    private Agenda mid;
    private Displayable telaanterior;
    private Command opcSalvar;
    private Command opcVoltar;
    private String URL = "http://localhost:8080/ServidorAgenda/Principal";
    private String ret;
    private TextField txtDescricao, txtData;

    public FrmEventoMSG(String title, Displayable disp, Agenda mid, String evento, String data) {
        super(title);
        this.mid = mid;
        this.opcVoltar = new Command("Voltar", Command.BACK, 0);
        this.addCommand(opcVoltar);
        this.setCommandListener(this);
        //append("Evento :" + evento, null);
        //append(msg, null);
        //append("Data:" + data, null);
    }

    

    public void commandAction(Command c, Displayable d) {
        Display display = Display.getDisplay(mid);
        if (c == opcVoltar) {
            display.setCurrent(telaanterior);
        } else if (c == opcSalvar) {
            Thread t = new Thread(new FrmEventoMSG.ConexaoWWW());
            t.start();
        }
    }

    private class ConexaoWWW implements Runnable {

        public void run() {
            HttpConnection httpConn = null;
            try {
                httpConn = (HttpConnection) Connector.open(URL, Connector.READ_WRITE);
                httpConn.setRequestMethod(HttpConnection.POST);
                httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                httpConn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
                httpConn.setRequestProperty("Content-Language", "pt-BR");
                httpConn.setRequestProperty("Accept", "application/octet-stream");
                httpConn.setRequestProperty("Connection", "close");
                OutputStream os = httpConn.openOutputStream();

                String login = "luis";
                String descricao;
                String data = null;

                descricao = txtDescricao.getString();
                data = txtData.getString();

                os.write(("acao=evento&acaoCRUD=salvar&login="+login+"&descricao=" + descricao + "&data=" + data).getBytes());
                os.close();
                
                EventoDAO eventoDAO = new EventoDAO();
                eventoDAO.open();
                eventoDAO.todos();
                eventoDAO.close();
                
                
                
                Display display = Display.getDisplay(mid);
                display.setCurrent(new FrmInicial("Menu", mid));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
